package com.yhgm.utils.timer;

import com.yhgm.constant.Constant;
import com.yhgm.mapper.TimerDao;
import com.yhgm.model.entity.Timer;
import lombok.extern.slf4j.Slf4j;
import org.quartz.*;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

@Component
@Slf4j
public class MyApplicationRunner implements ApplicationRunner {
    @Resource
    private QuartzUtil quartzUtil;
    @Resource
    private TimerDao timerDao;
    @Override
    public void run(ApplicationArguments args) throws Exception {
        // 在该方法中编写启动时执行的任务代码
        //System.out.println("应用程序已启动，执行 MyApplicationRunner 中的任务...");
        log.info("应用程序已启动，执行 MyApplicationRunner 中的任务...");
        //quartzUtil.add("pdf_del", Constant.CORN_PDF_DELETE);
        List<Timer> timerList=timerDao.getTimerIsRunning();
        for(Timer timer : timerList){
            if (timer.getTimerId().equals(Constant.BACKUPS_JOB_KEY)){
                if (quartzUtil.add(timer.getTimerId(),timer.getCorn())) {
                    log.info("已启动数据库备份任务");
                }
            }else if (timer.getTimerId().equals(Constant.FILE_DELETE_JOB_KEY)){
                if (quartzUtil.add(timer.getTimerId(),timer.getCorn())) {
                    log.info("已启动文件删除任务");
                }
            }else if (timer.getTimerId().equals(Constant.LOG_DELETE_JOB_KEY)){
                if (quartzUtil.add(timer.getTimerId(),timer.getCorn())) {
                    log.info("已启动日志自动清理任务");
                }
            }
        }
    }
}
